import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class TestFormWidget extends StatefulWidget {
  const TestFormWidget({super.key});

  @override
  State<StatefulWidget> createState() {
    return MFormState();
  }
}

class MFormState extends State<TestFormWidget> {
  TextEditingController _account = TextEditingController();
  TextEditingController _password = TextEditingController();
  GlobalKey _formKey = GlobalKey<FormState>();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("基础组件 表单 学习使用"),
        ),
        body: Form(
          key: _formKey,
          child: Column(
            children: [
              TextFormField(
                autofocus: true,
                controller: _account,
                decoration: InputDecoration(
                  labelText: "账号",
                  hintText: "请输入手机号/邮箱",
                  prefixIcon: Icon(Icons.person),
                  suffixIcon: IconButton(
                    icon: Icon(Icons.person),
                    onPressed: () {
                      print(" 点击了 》》》 ");
                    },
                  ),
                ),
                validator: (v) {
                  return v!.trim().isEmpty ? "用户名称不可为null" : null;
                },
              ),
              TextFormField(
                autofocus: true,
                obscureText: true,
                controller: _password,
                decoration: InputDecoration(
                  labelText: "密码",
                  hintText: "请输入密码",
                  prefixIcon: Icon(Icons.password),
                  suffixIcon: IconButton(
                    icon: Icon(Icons.password),
                    onPressed: () {
                      print(" 点击了 》》》 ");
                    },
                  ),
                ),
                validator: (v) {
                  return v!.trim().length > 5 ? null : "密码不能少于6位";
                },
              ),
              Padding(
                padding: EdgeInsets.only(top: 20.0),
                child: ElevatedButton(
                  onPressed: () {
                    print("点击登陆 1 ${(_formKey.currentState as FormState).validate()}");
                  },
                  child: Text(
                    "登陆",
                  ),
                ),
              )
            ],
          ),
        ));
  }
}
